package cn.doctorpda.conference.ui.search;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import cn.doctorpda.conference.R;
import cn.doctorpda.conference.adapter.SearchInfoAdapter;
import cn.doctorpda.conference.bean.DataReportInfo;
import cn.doctorpda.conference.bean.Msg;
import cn.doctorpda.conference.bean.search.PageSearchInfo;
import cn.doctorpda.conference.bean.search.SearchInfoItem;
import cn.doctorpda.conference.event.GetMessage;
import cn.doctorpda.conference.event.GetSearchKeyMessage;
import cn.doctorpda.conference.presenter.SearchActivityPresenter;
import cn.doctorpda.conference.ui.BaseFragment;
import cn.doctorpda.conference.ui.common.DetailActivity;
import cn.doctorpda.conference.utils.L;
import cn.doctorpda.conference.utils.T;
import cn.doctorpda.conference.view.callbackview.ISearchPageView;
import cn.doctorpda.conference.view.recyclerview.XRecylcerView;
import okhttp3.Call;

/**
 * Created by huangjinlong on 2016/5/17.
 */
public class SearchCommonFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, XRecylcerView.LoadingListener, ISearchPageView {

    private SwipeRefreshLayout checkRefreshLayout;
    private XRecylcerView checkXRecylcerView;
    private SearchInfoAdapter mAdapter ;
    private SearchActivityPresenter serVideoPresenter;
    private int pageNumber = 1;
    private String keyWord = "*";
    private String searchType;
    private String pageSize = "10";
    private View view;

    private LinearLayout ll_no_data;
    private TextView tv_no_data;
    private ImageView imv_no_data;

    public static SearchCommonFragment newInstance(String keyWord, String searchType) {
        SearchCommonFragment fragment = new SearchCommonFragment();
        Bundle bundle = new Bundle();
        bundle.putString("keyWord", keyWord);
        bundle.putString("searchType", searchType);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mAdapter= new SearchInfoAdapter();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view != null) {
            return view;
        }
        view = inflater.inflate(R.layout.fragment_info_common, container, false);
        initData();
        initView(view);
        EventBus.getDefault().register(this);
        return view;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        keyWord = getArguments().getString("keyWord");
        searchType = getArguments().getString("searchType");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onSearchKeyEvent(GetSearchKeyMessage messageEvent) {

        keyWord = messageEvent.getMessage();
        serVideoPresenter.GetSearchData("1", pageSize, keyWord, searchType);
    }


    @Override
    public String getPageName() {
        return "SearchCommonFragment";
    }

    private void initData() {

        serVideoPresenter = new SearchActivityPresenter(this);
        serVideoPresenter.GetSearchData("1", pageSize, keyWord, searchType);


    }

    private void initView(View view) {
        checkRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.info_refresh);
        checkXRecylcerView = (XRecylcerView) view.findViewById(R.id.info_loadmore);
        checkXRecylcerView.setHasFixedSize(true);
        checkXRecylcerView.setLoadingListener(this);
        checkXRecylcerView.setLoadingMoreEnabled(true);
        checkRefreshLayout.setOnRefreshListener(this);
        checkXRecylcerView.setLayoutManager(new GridLayoutManager(getContext(), 2));
        checkXRecylcerView.setAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();

        mAdapter.setOnItemClickListener(new SearchInfoAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, SearchInfoItem data) {
                L.i("地址url" + data.getId());
                DetailActivity.startActivity(getActivity(), data.getTitle(),data.getId(), data.getType(), data.getId());//id拼接url
            }
        });

        ll_no_data = (LinearLayout) view.findViewById(R.id.ll_no_data);
        tv_no_data = (TextView) view.findViewById(R.id.tv_no_data_show);
        imv_no_data = (ImageView) view.findViewById(R.id.imv_no_data_show);
    }

    @Override
    public void onLoadMore() {
        ++pageNumber;
        serVideoPresenter.GetSearchData(pageNumber + "", pageSize, keyWord, searchType);
    }

    @Override
    public void onRefresh() {
        pageNumber = 1;
        serVideoPresenter.GetSearchData("1", pageSize, keyWord, searchType);
        ll_no_data.setVisibility(View.INVISIBLE);
    }


    @Override
    public void setPageRefreshResult(Msg msg, PageSearchInfo data) {
        // 设置当前刷新数，当下拉刷新的时候置为0，上拉加载时已经初始化
        checkXRecylcerView.setPreviousTotal(0);
        // 设置是否还有更多
        checkXRecylcerView.setIsnomore(false);
        checkRefreshLayout.setRefreshing(false);
        mAdapter.clear();
        if (data != null) {
            if (data.getList().isEmpty()) {
                ll_no_data.setVisibility(View.VISIBLE);
                tv_no_data.setVisibility(View.VISIBLE);
                imv_no_data.setBackgroundResource(R.drawable.nodata);
                tv_no_data.setText("");
            } else {
                ll_no_data.setVisibility(View.INVISIBLE);
            }
            mAdapter.addAll(data.getList());
        } else {
            ll_no_data.setVisibility(View.VISIBLE);
            tv_no_data.setVisibility(View.INVISIBLE);
            imv_no_data.setBackgroundResource(R.drawable.shibai);
        }
    }

    @Override
    public void setPageLoadResult(Msg msg, PageSearchInfo data) {
        checkXRecylcerView.loadMoreComplete();
        if (data != null)
            mAdapter.addAll(data.getList());
    }

    @Override
    public void setPageError(Call call, Exception e) {
        mAdapter.clear();
        checkXRecylcerView.loadMoreComplete();
        ll_no_data.setVisibility(View.VISIBLE);
        imv_no_data.setBackgroundResource(R.drawable.shibai);
        tv_no_data.setVisibility(View.INVISIBLE);
        checkRefreshLayout.setRefreshing(false);
    }


}
